(function($) {
	$(document).ready(function() {
		$("#addLeagueButton").livequery('click',function() {
			var action = 'LeagueEditor_Controller/addLeague';
			var name = $("#newLeagueName").val();
			var id = $("#Form_EditForm_ID").val();

			//send ajax request to the page
			$.ajax({
				type: "POST",
				url: action,
				data: 'name='+ name + '&id=' + id,
				
				// create a new field
				success: function(response){
					if (response != '')
						$('#leagueTables').html(response);
				},
				
				// error creating new field
				error: function(request, text, error) {
					alert('error saving data');
				} 
			});			
			
			return false;
		});
		
		
		
		$('input[name*=addTeam]').livequery('click',function() {
			var attr = this.name.split('-');
			var leagueId = parseInt(attr[1]);
			var teamId = parseInt($(this).prev('select').val());
			
			if (leagueId && teamId) {
				var action = 'LeagueEditor_Controller/addTeamToLeague';
				$.ajax({
					type: "POST",
					url: action,
					data: 'leagueId='+ leagueId + '&teamId=' + teamId,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});					
			}

			
			return false;
		});

		
		$('input[name*=delFairPlayFoulPoints]').livequery('click',function() {
			var attr = this.name.split('-');
			var foulPointId = parseInt(attr[2]);
			
			if (foulPointId) {
				var action = 'LeagueEditor_Controller/deleteFairPlayPoint';
				$.ajax({
					type: "POST",
					url: action,
					data: 'FoulPointId='+ foulPointId,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});					
			}

			
			return false;
		});
		
		
		$('input[name*=addFairPlayFoulPoints]').livequery('click',function() {
			var attr = this.name.split('-');
			var leagueId = parseInt(attr[1]);
			var teamId = parseInt(attr[2]);
			var fP = parseInt($(this).prev('input').prev('input').val());
			var fM = $(this).prev('input').val();
			//var foulPoints = parseInt($(this).prev('input').prev('input').val());
			
			
			
			if (fP && fM && leagueId && teamId) {
				var action = 'LeagueEditor_Controller/addFairPlayFoulPointsToTeam';
				$.ajax({
					type: "POST",
					url: action,
					data: 'leagueId='+ leagueId + '&teamId=' + teamId + '&points=' + fP + '&desc=' + fM,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});
			}
			
			
			return false;
		});
		//
		$('input[name*=delFoulPoints]').livequery('click',function() {
			var attr = this.name.split('-');
			var foulPointId = parseInt(attr[2]);
			
			if (foulPointId) {
				var action = 'LeagueEditor_Controller/deleteFoulPoint';
				$.ajax({
					type: "POST",
					url: action,
					data: 'FoulPointId='+ foulPointId,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});					
			}

			
			return false;
		});
		
		
		$('input[name*=addFoulPoints]').livequery('click',function() {
			var attr = this.name.split('-');
			var leagueId = parseInt(attr[1]);
			var teamId = parseInt(attr[2]);
			var fP = parseInt($(this).prev('input').prev('input').val());
			var fM = $(this).prev('input').val();
			//var foulPoints = parseInt($(this).prev('input').prev('input').val());
			
			
			
			if (fP && fM && leagueId && teamId) {
				var action = 'LeagueEditor_Controller/addFoulPointsToTeam';
				$.ajax({
					type: "POST",
					url: action,
					data: 'leagueId='+ leagueId + '&teamId=' + teamId + '&points=' + fP + '&desc=' + fM,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});
			}
			
			
			return false;
		});
		
		$('input[name*=delLeague]').livequery('click',function() {
			var attr = this.name.split('-');
			var leagueId = parseInt(attr[1]);
			
			if (leagueId) {
				var action = 'LeagueEditor_Controller/deleteLeague';
				$.ajax({
					type: "POST",
					url: action,
					data: 'leagueId='+ leagueId,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});					
			}

			
			return false;
		});
		$('input[name*=delTeam]').livequery('click',function() {
			var attr = this.name.split('-');
			var leagueId = parseInt(attr[1]);
			var teamId = parseInt(attr[2]);
			
			if (leagueId && teamId) {
				var action = 'LeagueEditor_Controller/removeTeamFromLeague';
				$.ajax({
					type: "POST",
					url: action,
					data: 'leagueId='+ leagueId + '&teamId='+teamId,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});					
			}	
			return false;
		});
		
	});
})
(jQuery);